/* eslint-disable no-nested-ternary */
import React from 'react';
import PropTypes from 'prop-types';
import routes from './routes';
import cloneDeep from 'lodash/cloneDeep';
import {Button, Form, Row, Col, Card, Spin, Table, Checkbox} from 'antd';
import {getDetail, getAreas, getAreasSimple, getLastTime} from './api';
import {FORM_OPTIONS, TABLE, FILES_API, FORM_ROW_OPTIONS, DATE_FORMAT} from '../constants';
import Return from '../common/Return';
import FormItem from 'antd/lib/form/FormItem';
import {hasPermissions} from './utils';
import {PERMISSIONS} from './constants';
import {newOrUpdateOrderValidator} from './validator';
import {injectIntl, language} from './intl';
import WrappedSelect from '../common/WrappedSelect';
import DealerSelectPanel from '../common/DealerSelect/SelectEdit';
import TextInput from 'Shared/components/TextInput';
import InputAmount from '../common/InputAmount';
import WrappedDatePicker from '../common/WrappedDatePicker';
import LocalizeInputNumber from '../common/LocalizeInputNumber';
import {currency, lcKind, isYesNo, lcOfficialDraftType, lcPayMentDays, multiLevelApproveResultJudgeResult} from '../Enum';
import FileUpload from './FileUpload';
import SearchSelectPanel from '../common/DutyUnitSelect/SelectEdit';
import {conventEnumValueToString, formatDateTime, formatAmount} from '../utils';
import DescriptionList from 'ant-design-pro/lib/DescriptionList';
import moment from 'moment';
const {Description} = DescriptionList;
const datetime = moment(new Date()).format();
export class NewPanel extends React.PureComponent {
    state = {
        loading: false,
        hasOperat: false,
        validate: false,
        data: {
            brandId: this.props.initEnum.brands.length === 1 ? this.props.initEnum.brands[0].value : undefined
        },
        details: [],
        approveResultDetails: []
    };

    componentDidMount() {
        this.getDetailInfo();
    }
    
    componentWillUnmount() {
        this.isunmount = true;
    }
    getDetailInfo = () => {
        if(this.props.id) {
            this.setState(({
                loading: true
            }));
            getDetail(this.props.id)
                .then(res => {
                    if(res.ok) {
                        if(typeof res.data.isAllowBatch === 'boolean')
                            res.data.isAllowBatch = (res.data.isAllowBatch === true ? 1 : 0);
                        if(typeof res.data.isAllowTransfer === 'boolean')
                            res.data.isAllowTransfer = (res.data.isAllowTransfer === true ? 1 : 0);
                        this.setState({
                            loading: false,
                            data: res.data,
                            approveResultDetails: res.data.approveResultDetails,
                            details: this.props.page === 'approve' ? [{
                                id: '1',
                                name: this.props.intl.formatMessage({
                                    id: 'newPanel.form.issuePrice',
                                    defaultMessage: '开证金额'
                                }),
                                value: formatAmount(res.data.issuePrice),
                                select: false
                            }, {
                                id: '2',
                                name: this.props.intl.formatMessage({
                                    id: 'newPanel.form.shippingPortName',
                                    defaultMessage: '发货港'
                                }),
                                value: res.data.shippingPortName,
                                select: false
                            }, {
                                id: '3',
                                name: this.props.intl.formatMessage({
                                    id: 'newPanel.form.destinationPort',
                                    defaultMessage: '目的港'
                                }),
                                value: res.data.destinationPort,
                                select: false
                            }, {
                                id: '4',
                                name: this.props.intl.formatMessage({
                                    id: 'newPanel.form.lastLoadDate',
                                    defaultMessage: '最迟装船日期'
                                }),
                                value: formatDateTime(res.data.lastLoadDate, DATE_FORMAT),
                                select: false
                            }, {
                                id: '5',
                                name: this.props.intl.formatMessage({
                                    id: 'newPanel.form.dateOfExpiry',
                                    defaultMessage: '效期'
                                }),
                                value: formatDateTime(res.data.dateOfExpiry, DATE_FORMAT),
                                select: false
                            }, {
                                id: '6',
                                name: this.props.intl.formatMessage({
                                    id: 'newPanel.form.placeOfExpiry',
                                    defaultMessage: '效地'
                                }),
                                value: res.data.placeOfExpiry,
                                select: false
                            }, {
                                id: '7',
                                name: this.props.intl.formatMessage({
                                    id: 'newPanel.form.lcPayMentDays',
                                    defaultMessage: '信用证账期'
                                }),
                                value: conventEnumValueToString(lcPayMentDays, res.data.lcPayMentDays),
                                select: false
                            }, {
                                id: '8',
                                name: this.props.intl.formatMessage({
                                    id: 'newPanel.form.commodityDescription',
                                    defaultMessage: '货描'
                                }),
                                value: res.data.commodityDescription,
                                select: false
                            }] : [{
                                id: '9',
                                name: this.props.intl.formatMessage({
                                    id: 'newPanel.form.issueBank',
                                    defaultMessage: '开证银行'
                                }),
                                value: res.data.issueBank,
                                select: false
                            }, {
                                id: '10',
                                name: this.props.intl.formatMessage({
                                    id: 'newPanel.form.lcKind',
                                    defaultMessage: '信用证类型'
                                }),
                                value: conventEnumValueToString(lcKind, res.data.lcKind),
                                select: false
                            }, {
                                id: '11',
                                name: this.props.intl.formatMessage({
                                    id: 'newPanel.form.lcPayMentDays',
                                    defaultMessage: '信用证账期'
                                }),
                                value: conventEnumValueToString(lcPayMentDays, res.data.lcPayMentDays),
                                select: false
                            }]
                        });
                    } else
                        this.setState(({
                            loading: false
                        }));
                });
        }
    }
    getLetter = (condition, data) => {
        if(condition.id && data.brandId) {
            this.setState({loading: true});
            getLastTime({
                dealerId: condition.id,
                brandId: data.brandId,
            })
                .then(res => {
                    if(res.ok)
                        this.setState({
                            loading: false,
                            data: Object.assign({}, data, {
                                dealerId: condition.id,
                                dealerCode: condition.code,
                                dealerName: condition.name,
                                issueCountryId: res.data.issueCountryId,
                                issueCountryChineseName: res.data.issueCountryChineseName,
                                issueBank: res.data.issueBank,
                                currency: res.data.currency,
                                lcPayMentDays: res.data.lcPayMentDays,
                                issueDate: datetime,
                                isAllowBatch: res.data.issueCountryId ? (res.data.isAllowBatch === true ? 1 : 0) : isYesNo.是,
                                isAllowTransfer: res.data.issueCountryId ? (res.data.isAllowTransfer === true ? 1 : 0) : isYesNo.是,
                                shippingPortId: res.data.shippingPortId,
                                shippingPortCode: res.data.shippingPortCode,
                                shippingPortName: res.data.shippingPortName,
                                destinationPort: res.data.destinationPort,
                                lcKind: res.data.issueCountryId ? res.data.lcKind : lcKind['正常 L/C'],
                                dateOfExpiry: res.data.dateOfExpiry,
                                placeOfExpiry: res.data.placeOfExpiry,
                                lcOfficialDraftType: res.data.issueCountryId ? res.data.lcOfficialDraftType : lcOfficialDraftType.草本
                            })
                        });
                    else
                        this.setState(state => {
                            const newData = cloneDeep(state.data);
                            return {
                                hasOperat: true,
                                loading: false,
                                data: {
                                    ...newData,
                                    dealerId: condition.id,
                                    dealerCode: condition.code,
                                    dealerName: condition.name,
                                }
                            };
                        });
                });
        }
    }
    handleFilterChange = (value, name) => {
        const data = Object.assign({}, this.state.data, {
            [name]: value
        });
        this.setState({
            data,
            hasOperat: true
        });
    }
    removeFilePath = file => {
        const data = this.state.data;
        const index = data.attachments.findIndex(item => item.fileId === file.fileId);
        if(index === -1)
            return null;
        const attachments = data.attachments || [];
        attachments.splice(index, 1);
        this.setState({
            data: {
                ...data,
                attachments
            },
            hasOperat: true,
        });
    }
    onSelectCountry = value => {
        const data = Object.assign({}, this.state.data, {
            issueCountryId: value.id,
            issueCountryChineseName: value.chineseName
        });
        this.setState({
            data,
            hasOperat: true
        });
    }
    onClickReturnBtn = () => {
        this.props.onCancel();
    }
    
    onSubmit = () => {
        const isValid = newOrUpdateOrderValidator(this.state.data);
        if(!isValid) {
            this.setState({
                validate: true
            });
            return;
        }
        this.setState({
            loading: true,
        });
        this.props.onSubmit(this.state.data).then(() => {
            if(!this.isunmount)
                this.setState({loading: false});
        });
    }
    
    onSubmitReject = () => {
        this.setState({loading: true});
        if(this.props.page === 'approve')
            this.props.onSubmitApproveReject(this.state.data).then(() => this.setState({loading: false}));
        else this.props.onSubmitFinanceReject(this.state.data).then(() => this.setState({loading: false}));
    }

    onSubmitPass = () => {
        this.setState({loading: true});
        if(this.props.page === 'approve')
            this.props.onSubmitApprovePass(this.state.data).then(() => this.setState({loading: false}));
        else
            this.props.onSubmitFinancePass(this.state.data).then(() => this.setState({loading: false}));
    }
    onChangeUpload = value => {
        const data = this.state.data;
        const attachments = data.attachments || [];
        attachments.push(value[0]);
        this.setState({
            data: {
                ...data,
                attachments
            },
            hasOperat: true,
        });
    }
    onSelectDealer = (condition = {}) => {
        this.getLetter(condition, this.state.data);
    };
    onCheckSelect = e => {
        const checked = e.target.checked;
        const details = this.state.details;
        switch(e.target.id) {
            case '9':
                this.setState({
                    data: Object.assign({}, this.state.data, {
                        issueBankIsApprove: checked
                    })
                });
                break;
            case '10':
                this.setState({
                    data: Object.assign({}, this.state.data, {
                        lcKindIsApprove: checked
                    })
                });
                break;
            case '11':
                this.setState({
                    data: Object.assign({}, this.state.data, {
                        lcPayMentDaysFinanceIsApprove: checked
                    })
                });
                break;
            case '1':
                this.setState({
                    data: Object.assign({}, this.state.data, {
                        issuePriceIsApprove: checked
                    })
                });
                break;
            case '2':
                this.setState({
                    data: Object.assign({}, this.state.data, {
                        shippingPortIsApprove: checked
                    })
                });
                break;
            case '3':
                this.setState({
                    data: Object.assign({}, this.state.data, {
                        destinationPortIsApprove: checked
                    })
                });
                break;
            case '4':
                this.setState({
                    data: Object.assign({}, this.state.data, {
                        lastLoadDateIsApprove: checked
                    })
                });
                break;
            case '5':
                this.setState({
                    data: Object.assign({}, this.state.data, {
                        dateOfExpiryIsApprove: checked
                    })
                });
                break;
            case '6':
                this.setState({
                    data: Object.assign({}, this.state.data, {
                        placeOfExpiryIsApprove: checked
                    })
                });
                break;
            case '7':
                this.setState({
                    data: Object.assign({}, this.state.data, {
                        lcPayMentDaysMarketDepartmentIsApprove: checked
                    })
                });
                break;
            case '8':
                this.setState({
                    data: Object.assign({}, this.state.data, {
                        commodityDescriptionIsApprove: checked
                    })
                });
                break;
        }
        const tmp = details.map(item => {
            if(item.id === e.target.id)
                return Object.assign({}, item, {
                    select: checked
                });
            return item;
        });
        this.setState({
            details: tmp,
            hasOperat: true
        });
    }
    render() {
        const {data, details, approveResultDetails} = this.state;
        const dealerInfo = data.dealerId ? {
            id: data.dealerId,
            code: data.dealerCode,
            name: data.dealerName,
        } : null;
        const areaInfo = data.issueCountryId ? {
            id: data.issueCountryId,
            chineseName: data.issueCountryChineseName
        } : null;
        const validateBrand = !data.brandId && this.state.validate ? 'error' : null;
        const validateDealer = !data.dealerId && this.state.validate ? 'error' : null;
        const validateIssueDate = !data.issueDate && this.state.validate ? 'error' : null;
        const validateIssuePrice = !data.issuePrice && this.state.validate ? 'error' : null;
        const validateIssueCountry = !data.issueCountryId && this.state.validate ? 'error' : null;
        const validateIssueBank = !data.issueBank && this.state.validate ? 'error' : null;
        const validateDateOfExpiry = !data.dateOfExpiry && this.state.validate ? 'error' : null;
        const validateLcKind = !data.lcKind && this.state.validate ? 'error' : null;
        const validateCurrency = !data.currency && this.state.validate ? 'error' : null;
        const validateLcPayMentDays = !data.lcPayMentDays && this.state.validate ? 'error' : null;
        const validateLcOfficialDraftType = !data.lcOfficialDraftType && this.state.validate ? 'error' : null;
        const validateIsAllowBatch = !data.isAllowBatch && typeof data.isAllowBatch !== 'number' && this.state.validate ? 'error' : null;
        const {formatMessage} = this.props.intl;
        const {pagePermission, initEnum} = this.props;
        if(!data.id && initEnum.brands.length === 1)
            data.brandId = initEnum.brands[0].value;
        const submitable = hasPermissions(PERMISSIONS.add, pagePermission) ||
            hasPermissions(PERMISSIONS.update, pagePermission) ||
            hasPermissions(PERMISSIONS.approve, pagePermission) ||
            hasPermissions(PERMISSIONS.financeApprove, pagePermission);
        
        const colums = [{
            title: formatMessage({
                id: 'newPanel.column.name',
                defaultMessage: '标题'
            }),
            dataIndex: 'name',
            width: 200
        }, {
            title: formatMessage({
                id: 'newPanel.column.value',
                defaultMessage: '数据'
            }),
            dataIndex: 'value',
            width: 400
        }, {
            title: <span className="ant-form-item-required">{formatMessage({
                id: 'newPanel.column.select',
                defaultMessage: '选择'
            })}</span>,
            dataIndex: 'select',
            render: (text, record) => (
                <Checkbox
                    checked={record.select}
                    name="select"
                    id={record.id}
                    value={record.select}
                    onChange={this.onCheckSelect}>
                </Checkbox>
            )
        }];

        const columsapprove = [{
            title: formatMessage({
                id: 'newPanel.column.approveSequence',
                defaultMessage: '审核序号'
            }),
            dataIndex: 'approveSequence'
        }, {
            title: formatMessage({
                id: 'newPanel.column.approverCode',
                defaultMessage: '审核人编号'
            }),
            dataIndex: 'approverCode'
        }, {
            title: formatMessage({
                id: 'newPanel.column.approverName',
                defaultMessage: '审核人姓名'
            }),
            dataIndex: 'approverName'
        }, {
            title: formatMessage({
                id: 'newPanel.column.approveTime',
                defaultMessage: '审核时间'
            }),
            dataIndex: 'approveTime',
            render: text => formatDateTime(text, DATE_FORMAT)
        }, {
            title: formatMessage({
                id: 'newPanel.column.approveComment',
                defaultMessage: '审核意见'
            }),
            dataIndex: 'approveComment'
        }, {
            title: formatMessage({
                id: 'newPanel.column.approveResult',
                defaultMessage: '判定结果'
            }),
            dataIndex: 'approveResult',
            render: text => conventEnumValueToString(multiLevelApproveResultJudgeResult, text)
        }, {
            title: formatMessage({
                id: 'newPanel.column.nextOperation',
                defaultMessage: '下一步操作'
            }),
            dataIndex: 'nextOperation'
        }];

        const attachments = (data.attachments && data.attachments.length >= 0
            ? (<DescriptionList>
                {data.attachments.map(item => (<Description key={item.fileId}>
                    <a icon="download" download href={`${FILES_API.files}/${item.fileId}`} size="small">
                        {item.fileName}</a></Description>))}
            </DescriptionList>)
            : (<DescriptionList title={formatMessage({
                id: 'detailPanel.content.noAttachment',
                defaultMessage: '暂无附件'
            })}>
            </DescriptionList>));
        return (
            <div className="form-standard">
                <Spin spinning={this.state.loading}>
                    <Form>
                        <Card title={formatMessage({
                            id: 'newPanel.card.title',
                            defaultMessage: '基本信息'
                        })}>
                            <Row>
                                {initEnum.brands.length !== 1 && <Col {...FORM_OPTIONS.col}>
                                    <FormItem
                                        label={this.props.intl.formatMessage({
                                            id: 'newPanel.form.brand',
                                            defaultMessage: '品牌'
                                        })}
                                        {...FORM_OPTIONS.item} validateStatus={validateBrand} required>
                                        {this.props.id ? `(${data.brandCode})${data.brandName}`
                                            : <WrappedSelect
                                                allowClear
                                                name="brandId"
                                                value={data.brandId}
                                                options={initEnum.brands}
                                                onChange={this.handleFilterChange}/>
                                        }
                                    </FormItem>
                                </Col>}
                                <Col {...FORM_OPTIONS.col}>
                                    <FormItem
                                        label={this.props.intl.formatMessage({
                                            id: 'newPanel.form.dealerId',
                                            defaultMessage: '总代'
                                        })}
                                        {...FORM_OPTIONS.item} validateStatus={validateDealer} required>
                                        {this.props.id ? `(${data.dealerCode})${data.dealerName}`
                                            : <DealerSelectPanel
                                                value={dealerInfo}
                                                onChange={this.handleFilterChange}
                                                onSelect={this.onSelectDealer} />
                                        }
                                    </FormItem>
                                </Col>
                                <Col {...FORM_OPTIONS.col}>
                                    <FormItem
                                        label={this.props.intl.formatMessage({
                                            id: 'newPanel.form.lcCode',
                                            defaultMessage: '信用证号码 '
                                        })}
                                        {...FORM_OPTIONS.item}>
                                        {(this.props.page === 'approve' || this.props.page === 'finance') ? data.lcCode : <TextInput
                                            name="lcCode"
                                            value={data.lcCode}
                                            onBlur={this.handleFilterChange} />}
                                    </FormItem>
                                </Col>
                                <Col {...FORM_OPTIONS.col}>
                                    <FormItem
                                        label={this.props.intl.formatMessage({
                                            id: 'newPanel.form.issueCountry',
                                            defaultMessage: '开证国家'
                                        })}
                                        {...FORM_OPTIONS.item} validateStatus={validateIssueCountry} required>
                                        {(this.props.page === 'approve' || this.props.page === 'finance') ? data.issueCountryChineseName : <SearchSelectPanel
                                            value={areaInfo}
                                            isCountry={true}
                                            getDutyUnits={getAreas}
                                            getDutyUnitSimple={getAreasSimple}
                                            titleName={formatMessage({
                                                id: 'newPanel.form.issueCountry',
                                                defaultMessage: '开证国家'
                                            })}
                                            onSelect={this.onSelectCountry} />}
                                    </FormItem>
                                </Col>
                                <Col {...FORM_OPTIONS.col}>
                                    <FormItem
                                        label={this.props.intl.formatMessage({
                                            id: 'newPanel.form.issueBank',
                                            defaultMessage: '开证银行'
                                        })}
                                        {...FORM_OPTIONS.item} validateStatus={validateIssueBank} required>
                                        {(this.props.page === 'approve' || this.props.page === 'finance') ? data.issueBank : <TextInput
                                            name="issueBank"
                                            value={data.issueBank}
                                            onBlur={this.handleFilterChange} />}
                                    </FormItem>
                                </Col>
                                <Col {...FORM_OPTIONS.col}>
                                    <FormItem
                                        label={this.props.intl.formatMessage({
                                            id: 'newPanel.form.issuePrice',
                                            defaultMessage: '开证金额'
                                        })} {...FORM_OPTIONS.item} validateStatus={validateIssuePrice} required>
                                        {(this.props.page === 'approve' || this.props.page === 'finance') ? formatAmount(data.issuePrice) : <InputAmount
                                            name="issuePrice"
                                            value={data.issuePrice}
                                            onChange={this.handleFilterChange}/>}
                                    </FormItem>
                                </Col>
                                <Col {...FORM_OPTIONS.col}>
                                    <FormItem
                                        label={this.props.intl.formatMessage({
                                            id: 'newPanel.form.currency',
                                            defaultMessage: '币种'
                                        })} {...FORM_OPTIONS.item} validateStatus={validateCurrency} required>
                                        {(this.props.page === 'approve' || this.props.page === 'finance')
                                            ? conventEnumValueToString(currency, data.currency) : <WrappedSelect
                                                allowClear
                                                name="currency"
                                                value={data.currency}
                                                options={currency.toList(language)}
                                                onChange={this.handleFilterChange}/>}
                                    </FormItem>
                                </Col>
                                <Col {...FORM_OPTIONS.col}>
                                    <FormItem
                                        label={this.props.intl.formatMessage({
                                            id: 'newPanel.form.lcPayMentDays',
                                            defaultMessage: '信用证账期'
                                        })}
                                        {...FORM_OPTIONS.item} validateStatus={validateLcPayMentDays} required>
                                        {(this.props.page === 'approve' || this.props.page === 'finance')
                                            ? conventEnumValueToString(lcPayMentDays, data.lcPayMentDays) : <WrappedSelect
                                                allowClear
                                                name="lcPayMentDays"
                                                value={data.lcPayMentDays}
                                                options={lcPayMentDays.toList(language)}
                                                onChange={this.handleFilterChange}/>}
                                    </FormItem>
                                </Col>
                                <Col {...FORM_OPTIONS.col}>
                                    <FormItem
                                        label={this.props.intl.formatMessage({
                                            id: 'newPanel.form.issueDate',
                                            defaultMessage: '开证日期'
                                        })}
                                        {...FORM_OPTIONS.item} validateStatus={validateIssueDate} required>
                                        {(this.props.page === 'approve' || this.props.page === 'finance')
                                            ? formatDateTime(data.issueDate, DATE_FORMAT) : <WrappedDatePicker
                                                name="issueDate"
                                                value={data.issueDate}
                                                onChange={this.handleFilterChange} />}
                                    </FormItem>
                                </Col>
                                <Col {...FORM_OPTIONS.col}>
                                    <FormItem
                                        label={this.props.intl.formatMessage({
                                            id: 'newPanel.form.lastLoadDate',
                                            defaultMessage: '最迟装船日期'
                                        })}
                                        {...FORM_OPTIONS.item}>
                                        {this.props.page === 'finance' ? data.lastLoadDate : <WrappedDatePicker
                                            name="lastLoadDate"
                                            value={data.lastLoadDate}
                                            onChange={this.handleFilterChange} />}
                                    </FormItem>
                                </Col>
                                <Col {...FORM_OPTIONS.col}>
                                    <FormItem
                                        label={this.props.intl.formatMessage({
                                            id: 'newPanel.form.shippingPortId',
                                            defaultMessage: '起运港'
                                        })}
                                        {...FORM_OPTIONS.item}>
                                        {(this.props.page === 'approve' || this.props.page === 'finance') ? data.shippingPortName : <WrappedSelect
                                            allowClear
                                            name="shippingPortId"
                                            value={data.shippingPortId}
                                            options={initEnum.shippingPorts}
                                            onChange={this.handleFilterChange}/>}
                                    </FormItem>
                                </Col>
                                <Col {...FORM_OPTIONS.col}>
                                    <FormItem
                                        label={this.props.intl.formatMessage({
                                            id: 'newPanel.form.destinationPort',
                                            defaultMessage: '目的港'
                                        })}
                                        {...FORM_OPTIONS.item}>
                                        {(this.props.page === 'approve' || this.props.page === 'finance') ? data.destinationPort : <TextInput
                                            name="destinationPort"
                                            value={data.destinationPort}
                                            onBlur={this.handleFilterChange} />}
                                    </FormItem>
                                </Col>
                                <Col {...FORM_OPTIONS.col}>
                                    <FormItem
                                        label={this.props.intl.formatMessage({
                                            id: 'newPanel.form.isAllowBatch',
                                            defaultMessage: '是否允许分批'
                                        })}
                                        {...FORM_OPTIONS.item} validateStatus={validateIsAllowBatch} required>
                                        {(this.props.page === 'approve' || this.props.page === 'finance')
                                            ? (data.isAllowBatch ? this.props.intl.formatMessage({
                                                id: 'newPanel.form.yes',
                                                defaultMessage: '是'
                                            }) : this.props.intl.formatMessage({
                                                id: 'newPanel.form.no',
                                                defaultMessage: '否'
                                            })) : <WrappedSelect
                                                allowClear
                                                name="isAllowBatch"
                                                value={data.isAllowBatch}
                                                options={isYesNo.toList(language)}
                                                onChange={this.handleFilterChange}/>}
                                    </FormItem>
                                </Col>
                                <Col {...FORM_OPTIONS.col}>
                                    <FormItem
                                        label={this.props.intl.formatMessage({
                                            id: 'newPanel.form.isAllowTransfer',
                                            defaultMessage: '是否允许转运'
                                        })}
                                        {...FORM_OPTIONS.item}>
                                        {(this.props.page === 'approve' || this.props.page === 'finance')
                                            ? (data.isAllowTransfer ? this.props.intl.formatMessage({
                                                id: 'newPanel.form.yes',
                                                defaultMessage: '是'
                                            }) : this.props.intl.formatMessage({
                                                id: 'newPanel.form.no',
                                                defaultMessage: '否'
                                            })) : <WrappedSelect
                                                allowClear
                                                name="isAllowTransfer"
                                                value={data.isAllowTransfer}
                                                options={isYesNo.toList(language)}
                                                onChange={this.handleFilterChange}/>}
                                    </FormItem>
                                </Col>
                                <Col {...FORM_OPTIONS.col}>
                                    <FormItem label={formatMessage({
                                        id: 'newPanel.text.quantity',
                                        defaultMessage: '件数'
                                    })} {...FORM_OPTIONS.item}>
                                        {(this.props.page === 'approve' || this.props.page === 'finance') ? data.quantity : <LocalizeInputNumber
                                            min={0}
                                            name="quantity"
                                            value={data.quantity}
                                            onChange={this.handleFilterChange}/>}
                                    </FormItem>
                                </Col>
                                <Col {...FORM_OPTIONS.col}>
                                    <FormItem
                                        label={this.props.intl.formatMessage({
                                            id: 'newPanel.form.dateOfExpiry',
                                            defaultMessage: '效期'
                                        })}
                                        {...FORM_OPTIONS.item} validateStatus={validateDateOfExpiry} required>
                                        {(this.props.page === 'approve' || this.props.page === 'finance')
                                            ? formatDateTime(data.dateOfExpiry, DATE_FORMAT) : <WrappedDatePicker
                                                name="dateOfExpiry"
                                                value={data.dateOfExpiry}
                                                onChange={this.handleFilterChange} />}
                                    </FormItem>
                                </Col>
                                <Col {...FORM_OPTIONS.col}>
                                    <FormItem
                                        label={this.props.intl.formatMessage({
                                            id: 'newPanel.form.placeOfExpiry',
                                            defaultMessage: '效地'
                                        })}
                                        {...FORM_OPTIONS.item}>
                                        {(this.props.page === 'approve' || this.props.page === 'finance') ? data.placeOfExpiry : <TextInput
                                            name="placeOfExpiry"
                                            value={data.placeOfExpiry}
                                            onBlur={this.handleFilterChange} />}
                                    </FormItem>
                                </Col>
                                <Col {...FORM_OPTIONS.col}>
                                    <FormItem
                                        label={this.props.intl.formatMessage({
                                            id: 'newPanel.form.lcOfficialDraftType',
                                            defaultMessage: '正本草本'
                                        })}
                                        {...FORM_OPTIONS.item} validateStatus={validateLcOfficialDraftType} required>
                                        {(this.props.page === 'approve' || this.props.page === 'finance')
                                            ? conventEnumValueToString(lcOfficialDraftType, data.lcOfficialDraftType) : <WrappedSelect
                                                allowClear
                                                name="lcOfficialDraftType"
                                                value={data.lcOfficialDraftType}
                                                options={lcOfficialDraftType.toList(language)}
                                                onChange={this.handleFilterChange}/>}
                                    </FormItem>
                                </Col>
                                <Col {...FORM_OPTIONS.col}>
                                    <FormItem
                                        label={this.props.intl.formatMessage({
                                            id: 'newPanel.form.lcKind',
                                            defaultMessage: '信用证类型'
                                        })}
                                        {...FORM_OPTIONS.item} validateStatus={validateLcKind} required>
                                        {this.props.id ? conventEnumValueToString(lcKind, data.lcKind) : <WrappedSelect
                                            allowClear
                                            name="lcKind"
                                            value={data.lcKind}
                                            options={lcKind.toList(language)}
                                            onChange={this.handleFilterChange}/>}
                                    </FormItem>
                                </Col>
                            </Row>
                            <Row>
                                <Col>
                                    <FormItem
                                        label={this.props.intl.formatMessage({
                                            id: 'newPanel.form.commodityDescription',
                                            defaultMessage: '货描'
                                        })}
                                        {...FORM_ROW_OPTIONS.item}>
                                        {(this.props.page === 'approve' || this.props.page === 'finance') ? data.commodityDescription : <TextInput
                                            name="commodityDescription"
                                            type="textarea"
                                            value={data.commodityDescription}
                                            onBlur={this.handleFilterChange} />}
                                    </FormItem>
                                </Col>
                            </Row>
                            <Row>
                                <Col>
                                    <FormItem
                                        label={this.props.intl.formatMessage({
                                            id: 'newPanel.form.remark',
                                            defaultMessage: '备注'
                                        })}
                                        {...FORM_ROW_OPTIONS.item}>
                                        <TextInput
                                            name="remark"
                                            type="textarea"
                                            value={data.remark}
                                            onBlur={this.handleFilterChange} />
                                    </FormItem>
                                </Col>
                            </Row>
                        </Card>
                        {(this.props.page === 'approve' || this.props.page === 'finance') &&
                            <div><Card title={formatMessage({
                                id: 'newPanel.card.approveResult',
                                defaultMessage: '审核记录'
                            })}>
                                <Table
                                    className="white-space-nowrap"
                                    columns={columsapprove}
                                    rowKey="id"
                                    pagination={false}
                                    dataSource={approveResultDetails}
                                    {...TABLE} />
                            </Card>
                            <Card title={formatMessage({
                                id: 'newPanel.card.approvetitle',
                                defaultMessage: '审核项目'
                            })}>
                                <Table
                                    className="white-space-nowrap"
                                    columns={colums}
                                    rowKey="id"
                                    pagination={false}
                                    dataSource={details}
                                    {...TABLE} />
                            </Card>
                            <Card title={formatMessage({
                                id: 'newPanel.card.approve',
                                defaultMessage: '审核区域'
                            })}>
                                <Row>
                                    <Col>
                                        <FormItem
                                            label={this.props.intl.formatMessage({
                                                id: 'newPanel.form.approveComment',
                                                defaultMessage: '审核意见'
                                            })}
                                            {...FORM_ROW_OPTIONS.item}>
                                            <TextInput
                                                name="approveComment"
                                                type="textarea"
                                                value={data.approveComment}
                                                onBlur={this.handleFilterChange} />
                                        </FormItem>
                                    </Col>
                                </Row>
                            </Card></div>
                        }
                        <Card title={formatMessage({
                            id: 'newPanel.title.files',
                            defaultMessage: '附件清单'
                        })}>
                            {(this.props.page === 'approve' || this.props.page === 'finance')
                                ? attachments : <Row>
                                    <Col {...FORM_OPTIONS.col}>
                                        <FileUpload onChange={this.onChangeUpload}
                                            onRemove={this.removeFilePath}
                                            fileList={data.attachments || []}/>
                                    </Col>
                                </Row>}
                        </Card>
                        <Card type="inner">
                            <Row>
                                <Col disabled={!submitable} className="col-align-right">
                                    {(this.props.page !== 'approve' && this.props.page !== 'finance') && <Button key="submit" loading={this.state.loading}
                                        onClick={this.onSubmit} type="primary">{formatMessage({
                                            id: 'newPanel.btn.submit',
                                            defaultMessage: '保存'
                                        })}</Button>}
                                    {(this.props.page === 'approve' || this.props.page === 'finance') && <div><Button
                                        onClick={this.onSubmitReject}
                                        // disabled={!submitable}
                                        loading={this.state.loading}>
                                        {formatMessage({
                                            id: 'approvePanel.operation.onSubmitReject',
                                            defaultMessage: '审核不通过'
                                        })}
                                    </Button>
                                    <Button
                                        type="primary"
                                        onClick={this.onSubmitPass}
                                        // disabled={!submitable}
                                        loading={this.state.loading}>
                                        {formatMessage({
                                            id: 'approvePanel.operation.onSubmitPass',
                                            defaultMessage: '审核通过'
                                        })}
                                    </Button></div>}
                                </Col>
                            </Row>
                        </Card>
                    </Form>
                </Spin>
                <div className="page-toolbar">
                    <Return
                        onConfirm={this.onClickReturnBtn}
                        disabled={this.state.loading}
                        showWarning={this.state.hasOperat} />
                </div>
            </div>
        );
    }
}

NewPanel.propTypes = {
    initEnum: PropTypes.object.isRequired,
    onCancel: PropTypes.func.isRequired,
    onSubmit: PropTypes.func.isRequired,
    onSubmitApprovePass: PropTypes.func.isRequired,
    onSubmitApproveReject: PropTypes.func.isRequired,
    onSubmitFinancePass: PropTypes.func.isRequired,
    onSubmitFinanceReject: PropTypes.func.isRequired,
    history: PropTypes.object,
    intl: PropTypes.object,
    page: PropTypes.string,
    pagePermission: PropTypes.array,
};
import {connect} from 'react-redux';
import {
    onSubmit,
    tableSearch,
    onSubmitApprovePass,
    onSubmitApproveReject,
    onSubmitFinancePass,
    onSubmitFinanceReject
} from './actions';
import {selectorFactory} from 'Shared/utils/immutableToJsSelectorFactory';
const getPagePermission = selectorFactory(['page', 'domainData', 'permissions', 'data']);
import {createSelector} from 'reselect';

const getinitEnumData = createSelector(
    state => state.getIn(['page', 'domainData', 'initData']),
    init => {
        const initData = init.toJS();
        const brands = initData.brands ? initData.brands.map(item => ({
            text: `${'('}${item.code}${')'}${item.name}`,
            value: item.id
        })) : [];
        const shippingPorts = initData.shippingPorts ? initData.shippingPorts.map(item => ({
            text: `${'('}${item.code}${')'}${item.name}`,
            value: item.id
        })) : [];
        return {
            brands,
            shippingPorts
        };
    }
);
const mapStateToProps = state => ({
    pagePermission: getPagePermission(state),
    initEnum: getinitEnumData(state),
});

const mapDispatchToProps = (dispatch, props) => {
    const ownProps = {
        onSubmit: data => dispatch(onSubmit(data)).then(result => {
            if(result.success) {
                dispatch(tableSearch());
                props.history.push(routes.query.url());
            }
        }),
        onSubmitApprovePass: data => dispatch(onSubmitApprovePass(data)).then(result => {
            if(result.success) {
                dispatch(tableSearch());
                props.history.push(routes.query.url());
            }
        }),
        onSubmitApproveReject: data => dispatch(onSubmitApproveReject(data)).then(result => {
            if(result.success) {
                dispatch(tableSearch());
                props.history.push(routes.query.url());
            }
        }),
        onSubmitFinancePass: data => dispatch(onSubmitFinancePass(data)).then(result => {
            if(result.success) {
                dispatch(tableSearch());
                props.history.push(routes.query.url());
            }
        }),
        onSubmitFinanceReject: data => dispatch(onSubmitFinanceReject(data)).then(result => {
            if(result.success) {
                dispatch(tableSearch());
                props.history.push(routes.query.url());
            }
        }),
        onCancel: () => props.history.push(routes.query.url()),
    };
    return ownProps;
};

export default connect(mapStateToProps, mapDispatchToProps)(injectIntl(NewPanel));
